package com.fare.taluo.application.impl.crawl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.crawl.TaskQueueLogApplication;
import com.fare.taluo.core.domain.crawl.TaskQueueLogEntity;
import com.fare.taluo.core.domain.crawl.statistics.StatisticsTotalDetailEntity;
import com.fare.taluo.core.domain.crawl.statistics.StatisticsTotalEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.StatisticsTotalDetailEntityVO;
import com.fare.taluo.vo.core.StatisticsTotalEntityVO;
import com.fare.taluo.vo.core.TaskRecordEntityVO;

@Transactional
@Scope("singleton")
@Service("taskQueueLogApplication")
public class TaskQueueLogApplicationImpl implements TaskQueueLogApplication {

	private String todayStr=DateUtils.getDateStr("yyyy-MM-dd");
	
	@Override
	public List<TaskRecordEntityVO> findAllTaskRecord(String fromCityOrg,String toCityOrg,String grabChannel,Date startTime,Date endTime,String status,String sortorder,String sortname,int page,int pagesize) {
		TaskQueueLogEntity repository = new TaskQueueLogEntity();

		List<TaskQueueLogEntity> list = repository.getTaskRecordList(fromCityOrg, toCityOrg, grabChannel, startTime, endTime, status,sortorder,sortname,page,pagesize);
		
		List<TaskRecordEntityVO> listVO = new ArrayList<TaskRecordEntityVO>();
		
		if(list!=null){
			if(list.size()>0){
				for (TaskQueueLogEntity entity : list) {
					
					TaskRecordEntityVO vo=new TaskRecordEntityVO();
					
					 vo.setCreateTime(entity.getCreateTime());
					 vo.setUuid(entity.getId());
					 vo.setTaskid(entity.getTaskId());
					 vo.setStatus(entity.getStatus()+"");
					 vo.setCrawlCount(entity.getCrawlCount());
					 vo.setStartTime(entity.getStartTime());
					 vo.setEndTime(entity.getEndTime());
					 vo.setRemarks(entity.getRemarks());
					 vo.setRecordName(entity.getRecordName());
					 vo.setFromCityName(entity.getFromCityName());
					 vo.setFromCityOrg(entity.getFromCity());
					 vo.setToCityName(entity.getToCityName());
					 vo.setToCityOrg(entity.getToCity());
					 vo.setGrabDate(entity.getFlightDate());
					 vo.setGrabChannel(entity.getGrabChannel());
					 vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
					 vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+entity.getToCityName()+"("+entity.getToCity()+")");
					 vo.setSourceRowkey(entity.getSourceRowkey());
					 vo.setSourceDataSize(entity.getSourceDataSize());
					 listVO.add(vo);
					
				} 
			} 
		}
		return listVO;
	}

	

	
	private String countWasteTime(Date now,Date date){
		if(now!=null&&date!=null){
			   long l=now.getTime()-date.getTime(); 

			   long day=l/(24*60*60*1000); 

			   long hour=l/(60*60*1000)-day*24; 

			   long min=l/(60*1000)-day*24*60-hour*60; 

			   long s=l/1000-day*24*60*60-hour*60*60-min*60;
 
			   //System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒"); 
			   return (day*24*60*60+hour*60*60+min*60+s)+"秒";
		}
		
		return null;
	}
	//日期类型天转换成Long型秒
	private String changeTime(Date now){
		if(now!=null){
			   long l=now.getTime(); 
			   
			   long day=l/(24*60*60*1000); 

			   long hour=l/(60*60*1000)-day*24; 

			   long min=l/(60*1000)-day*24*60-hour*60; 

			   long s=l/1000-day*24*60*60-hour*60*60-min*60;
 
			   //System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒"); 
			   return (day*24*60*60+hour*60*60+min*60+s)+"秒";
		}
		
		return null;
	}

	
	@Override
	public List<TaskRecordEntityVO> findAll() {

		TaskQueueLogEntity repository = new TaskQueueLogEntity();
		List<TaskQueueLogEntity> list=new ArrayList<TaskQueueLogEntity>();
		try{
		list = repository.findAll();
		}catch(Exception e){
			e.printStackTrace();
		}
		List<TaskRecordEntityVO> listVO = new ArrayList<TaskRecordEntityVO>();
		if(list!=null){
			if(list.size()>0){
				for(TaskQueueLogEntity entity:list){
					TaskRecordEntityVO vo=new TaskRecordEntityVO();
					vo.setCrawlCount(entity.getCrawlCount());
					vo.setEndTime(entity.getEndTime());
					vo.setFromCityOrg(entity.getFromCity());
					vo.setFromCityName(entity.getFromCityName());
					vo.setGrabChannel(entity.getGrabChannel());
					vo.setGrabDate(entity.getFlightDate());
					vo.setRecordName(entity.getRecordName());
					vo.setRemarks(entity.getRemarks());
					vo.setStartTime(entity.getStartTime());
					vo.setStatus(entity.getStatus()+"");
					vo.setTaskid(entity.getTaskId());
					vo.setUuid(entity.getId());
					vo.setToCityOrg(entity.getToCity());
					vo.setToCityName(entity.getToCityName());
					vo.setSourceRowkey(entity.getSourceRowkey());
					vo.setSourceDataSize(entity.getSourceDataSize());
					listVO.add(vo);
				}
			}
		}
		return listVO;
	}

	@Override
	public List<TaskRecordEntityVO> findAllTaskRecordBygTime(Date beginTime,Date endTime,
			String gabd,String status) {
		TaskQueueLogEntity repository = new TaskQueueLogEntity();
		List<TaskQueueLogEntity> list=new ArrayList<TaskQueueLogEntity>();
		try{
		list = repository.getTaskRecordByGDate(beginTime,endTime,gabd,status);
		List<TaskRecordEntityVO> listVO = new ArrayList<TaskRecordEntityVO>();
		if(list!=null){
			if(list.size()>0){
				for(TaskQueueLogEntity entity:list){
					TaskRecordEntityVO vo=new TaskRecordEntityVO();
					vo.setCrawlCount(entity.getCrawlCount());
					vo.setEndTime(entity.getEndTime());
					vo.setFromCityOrg(entity.getFromCity());
					vo.setFromCityName(entity.getFromCityName());
					vo.setGrabChannel(entity.getGrabChannel());
					vo.setGrabDate(entity.getFlightDate());
					vo.setRecordName(entity.getRecordName());
					vo.setRemarks(entity.getRemarks());
					vo.setStartTime(entity.getStartTime());
					vo.setStatus(entity.getStatus()+"");
					vo.setTaskid(entity.getTaskId());
					vo.setUuid(entity.getId());
					vo.setToCityOrg(entity.getToCity());
					vo.setToCityName(entity.getToCityName());
					vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
							  entity.getToCityName()+"("+entity.getToCity()+")");
					vo.setSourceRowkey(entity.getSourceRowkey());
					vo.setSourceDataSize(entity.getSourceDataSize());
					listVO.add(vo);
				}
			}
		}
			return listVO;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int getTotalTaskRecordListByCodition(
			String fromCityOrg, String toCityOrg, String grabChannel,
			Date startTime, Date endTime, String status) {
		TaskQueueLogEntity repository = new TaskQueueLogEntity();
		return repository.getTotalTaskRecordListByCodition(fromCityOrg, toCityOrg, grabChannel, startTime, endTime, status);
	}


	//引擎统计 ，total详细信息
/*	@Override
	// for webServices
	public List<StatisticsTotalEntityVO> getTotalRecordByWS(TaskModel[] models) {
		int modelSizes=models.length;
		if(modelSizes>0){
			List<StatisticsTotalEntityVO> listVo =new ArrayList<StatisticsTotalEntityVO>();
			for(TaskModel entity:models){
				StatisticsTotalEntityVO vo=new StatisticsTotalEntityVO();
				vo.setFromCityName(entity.getFromCityName());
				vo.setGrabchannel(entity.getGrabChannel());
				vo.setGrabChannelID(entity.getGrabChannelId());
				vo.setToCityName(entity.getToCityName());
				vo.setStatus(entity.getStatus().longValue());
				vo.setGrabdate(todayStr);
				vo.setFromCity(entity.getFromCity());
				vo.setToCity(entity.getToCity());
//				vo.setTotal(vo.getFailNum()+vo.getSuccessNum());
				vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+entity.getToCityName()+"("+entity.getToCity()+")");
				vo.setSourceDataSize(entity.getSourceDataSize().longValue());
				if(entity.getStatus()==3){
					vo.setSuccessNum(1l);
				}if(entity.getStatus()!=3){
					vo.setFailNum(1l);
				} if(entity.getStatus()==-2){ 	// -2L表示      抓取失败
					vo.setSecondFail(1l);
				}if(entity.getStatus()==-3){ 	//-3表示    解析失败  
					vo.setThirdFail(1l);
				}if(entity.getStatus()==-4){		//-4表示     其他失败
					vo.setForthFail(1l);
				} if(entity.getStatus()==-10){	//-10表示  示定义失败
					vo.setOtherFail(1l);
				}if(entity.getStatus()==0){      //  0 表示 等待
					vo.setWaiting(1l);
				}if(entity.getStatus()== 1){      //　1　启动中
					vo.setStarting(1l);
				}if(entity.getStatus()==2){      // 2  执行中
					vo.setWorking(1l);
				}
				vo.setTotal(1l);
				listVo.add(vo);
			}
			return pingjie(listVo);
		}
		return null;
	}
	*/
	
	@Override
	//for database
	public List<StatisticsTotalEntityVO> getTotalRecordByDateAndChannel(String grabdate, String grabchannelID) throws Exception {
		StatisticsTotalEntity tqe = new StatisticsTotalEntity();
		List<StatisticsTotalEntityVO> listVO=new ArrayList<StatisticsTotalEntityVO>();
		List<StatisticsTotalEntity> list=new ArrayList<StatisticsTotalEntity>();
		list=tqe.getTotalRecordByDateAndChannel(grabdate, grabchannelID);
		for (StatisticsTotalEntity entity : list) {
			StatisticsTotalEntityVO vo=new StatisticsTotalEntityVO();
			vo.setFromCityName(entity.getFromCityName());
			vo.setGrabchannel(entity.getGrabchannel());
			vo.setGrabChannelID(entity.getGrabchannelID());
			vo.setActualNum(entity.getActualNum());
			vo.setAllbyteLength(entity.getAllbyteLength());
			vo.setSuccessNum(entity.getSuccessNum());
			vo.setToCityName(entity.getToCityName());
			vo.setStatus(entity.getStatus());
			vo.setGrabdate(entity.getGrabdate());
			vo.setFromCity(entity.getFromCity());
			vo.setToCity(entity.getToCity());
			vo.setFailNum(entity.getFailNum());
//			vo.setTotal(vo.getFailNum()+vo.getSuccessNum());
			vo.setTotal(entity.getTotal());
			vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
					entity.getToCityName()+"("+entity.getToCity()+")");
//			vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
			vo.setMaxTime(entity.getMaxTime());
			vo.setMinTime(entity.getMinTime());
			vo.setSourceDataSize(entity.getSourceDataSize());
			vo.setAvgTime(avgTime(entity.getMaxTime(),entity.getMinTime()));
			
			if(entity.getStatus()==-2l){ 			// -2L表示      抓取失败
				vo.setSecondFail(entity.getFailNum());
			}else if(entity.getStatus()==-3l){ 	//-3表示    解析失败  
				vo.setThirdFail(entity.getFailNum());
			}else if(entity.getStatus()==-4l){		//-4表示     其他失败
				vo.setForthFail(entity.getFailNum());
			}else if(entity.getStatus()==-10l){	//-10表示  示定义失败
				vo.setOtherFail(entity.getFailNum());
			}else if(entity.getStatus()==0l){      //  0 表示 等待
				vo.setWaiting(entity.getFailNum());
			}else if(entity.getStatus()== 1l){      //　1　启动中
				vo.setStarting(entity.getFailNum());
			}else if(entity.getStatus()==2l){      // 2  执行中
				vo.setWorking(entity.getFailNum());
			}
			
			listVO.add(vo);
		}
//		return listVO;
		return pingjie(listVO);
	}
	
	
	
	//pingjie
	private List pingjie(List<StatisticsTotalEntityVO> list){
		Map<String,StatisticsTotalEntityVO> map=new LinkedHashMap<String,StatisticsTotalEntityVO>();
		for (int i = 0; i < list.size(); i++) {
			StatisticsTotalEntityVO old=list.get(i);
			String key=old.getGrabchannel()+"--"+old.getGrabdate()+"--"+old.getFromCity()+"--"+old.getToCity();//拼装成一个key
			if(map.get( key )!=null){//判断已经存在同个日期，同个渠道，成功数相同，实际抓取数相同，同一出发城市，同一到到达城市 的数据
				int k=1;
				k++;
				StatisticsTotalEntityVO newVo=map.get(key);//将存在的数据取出来
				  //累加
				  Long total=nullToLong(old.getTotal(),newVo.getTotal());
				  Long failNum=nullToLong(old.getFailNum(),newVo.getFailNum());
				  Long secondFail=nullToLong(old.getSecondFail(),newVo.getSecondFail());
				  Long thirdFail=nullToLong(old.getThirdFail(),newVo.getThirdFail());
				  Long forthFail=nullToLong(old.getForthFail(),newVo.getForthFail());
				  Long otherFail=nullToLong(old.getOtherFail(),newVo.getOtherFail());
				  Long waiting=nullToLong(old.getWaiting(),newVo.getWaiting());
				  Long starting=nullToLong(old.getStarting(),newVo.getStarting());
				  Long working=nullToLong(old.getWorking(),newVo.getWorking());
				  Long actualNum=nullToLong(old.getActualNum(), newVo.getActualNum());
				  Long successNum=nullToLong(old.getSuccessNum(), newVo.getSuccessNum());
				  Long maxTime=nullToLong(old.getMaxTime(), newVo.getMaxTime());
				  Long minTime=nullToLong(old.getMinTime(), newVo.getMinTime());
				  //newVo.setTotal(total-newVo.getSuccessNum());//total在数据库中多加了一遍成功数
				  newVo.setTotal(total);
				  newVo.setFailNum(failNum);
				  newVo.setSecondFail(secondFail);
				  newVo.setThirdFail(thirdFail);
				  newVo.setForthFail(forthFail);
				  newVo.setOtherFail(otherFail);
				  newVo.setWaiting(waiting);
				  newVo.setStarting(starting);
				  newVo.setWorking(working);
				  newVo.setMaxTime(maxTime/k);
				  newVo.setMinTime(minTime/k);
				  newVo.setActualNum(actualNum/k);
				  newVo.setSuccessNum(successNum);
				  
	              map.put(key,newVo);
	       }else{
	          map.put(key,old);
	       }
		}
		Object s[] = map.keySet().toArray();
		for(int i = 0; i < map.size(); i++) {
			StatisticsTotalEntityVO stu=map.get(s[i]);
		}
		List voList = new ArrayList();
		Iterator iter = map.entrySet().iterator();  //获得map的Iterator
		while(iter.hasNext()) {
			Entry entry = (Entry)iter.next();
			voList.add(entry.getValue());
		}
		return voList;
	}
		//判断类型为空，赋值0
		private Long nullToLong(Long a,Long b){
			if(a!=null&&b!=null)
				return a+b;
			if(a==null & b==null)//这里用& 和  && 小心点
			    return 0l;
			if(a==null & b!=null)
				return b;
			if(b==null & a!=null)
				return a;
			return 0l;
		}
		
		//平均时间
		private Long avgTime(Long a,Long b){
			if(a!=null&&b!=null)
				return (a+b)/2;
			if(a==null & b==null)//这里用& 和  && 小心点
			    return 0l;
			if(a==null & b!=null)
				return b/2;
			if(b==null & a!=null)
				return a/2;
			return 0l;
		}
		
		//total
		private Long countadd(Long a,Long b){
			if(a!=null&&b!=null)
				return a+b;
			if(a==null & b==null)//这里用& 和  && 小心点
			    return 0l;
			if(a==null & b!=null)
				return b;
			if(b==null & a!=null)
				return a;
			return 0l;
		}
	
		//引擎统计——总数 
		//for WS
		/*@Override
		public List<StatisticsTotalDetailEntityVO> getTotalAllByWS(TaskModel[] models) {
			int modelSizes=models.length;
			if(modelSizes>0){
				List<StatisticsTotalDetailEntityVO> listVo =new ArrayList<StatisticsTotalDetailEntityVO>();
				for(TaskModel entity:models){
					StatisticsTotalDetailEntityVO vo=new StatisticsTotalDetailEntityVO();
					vo.setFlightDate(entity.getFlightDate());
					vo.setFromCityName(entity.getFromCityName());
					vo.setGrabchannel(entity.getGrabChannel());
					vo.setGrabChannelID(entity.getGrabChannelId());
					vo.setToCityName(entity.getToCityName());
					vo.setStatus(entity.getStatus().longValue());
					vo.setGrabdate(todayStr);
					vo.setFromCity(entity.getFromCity());
					vo.setToCity(entity.getToCity());
//					vo.setTotal(vo.getFailNum()+vo.getSuccessNum());
					vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+entity.getToCityName()+"("+entity.getToCity()+")");
					vo.setSourceDataSize(entity.getSourceDataSize().longValue());
					if(entity.getStatus()==3){
						vo.setSuccessNum(1l);
					}if(entity.getStatus()!=3){
						vo.setFailNum(1l);
					} if(entity.getStatus()==-2){ 	// -2L表示      抓取失败
						vo.setSecondFail(1l);
					}if(entity.getStatus()==-3){ 	//-3表示    解析失败  
						vo.setThirdFail(1l);
					}if(entity.getStatus()==-4){		//-4表示     其他失败
						vo.setForthFail(1l);
					} if(entity.getStatus()==-10){	//-10表示  示定义失败
						vo.setOtherFail(1l);
					}if(entity.getStatus()==0){      //  0 表示 等待
						vo.setWaiting(1l);
					}if(entity.getStatus()== 1){      //　1　启动中
						vo.setStarting(1l);
					}if(entity.getStatus()==2){      // 2  执行中
						vo.setWorking(1l);
					}
					vo.setTotal(1l);
					listVo.add(vo);
				}
				return pingjie2(listVo);
			}
			return null;
		}*/
		
		//for DB
		@Override
		public List<StatisticsTotalDetailEntityVO> getTotalAllByDateAndChannel(
				String grabdate, String grabchannelID,String fromCity,String toCity) {
			StatisticsTotalDetailEntity tqe = new StatisticsTotalDetailEntity();
			List<StatisticsTotalDetailEntityVO> listVO=new ArrayList<StatisticsTotalDetailEntityVO>();
			List<StatisticsTotalDetailEntity> list=new ArrayList<StatisticsTotalDetailEntity>();
			list=tqe.getTotalAllByFromAndTo(grabdate, grabchannelID,fromCity,toCity);
			for (StatisticsTotalDetailEntity entity : list) {
				StatisticsTotalDetailEntityVO vo=new StatisticsTotalDetailEntityVO();
				vo.setFlightDate(entity.getFlightdate());
				vo.setFromCity(entity.getFromCity());
				vo.setFromCityName(entity.getFromCityName());
				vo.setToCity(entity.getToCity());
				vo.setToCityName(entity.getToCityName());				
				vo.setGrabchannel(entity.getGrabchannel());
				vo.setMaxTime(entity.getMaxTime());
				vo.setMinTime(entity.getMinTime());
//				
				vo.setSuccessNum(entity.getSuccessNum());
				vo.setFailNum(entity.getFailNum());
//				vo.setActualNum(entity.getActualNum());
				vo.setStatus(entity.getStatus());
				vo.setAllbyteLength(entity.getAllbyteLength());
				vo.setId(entity.getId());
				vo.setToCityName(entity.getToCityName());
				vo.setAvgTime(avgTime(entity.getMaxTime(),entity.getMinTime()));
				vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
						entity.getToCityName()+"("+entity.getToCity()+")");
				vo.setSourceDataSize(entity.getSourceDataSize());
				vo.setTotal(countadd(entity.getFailNum(), entity.getSuccessNum()));
				if(entity.getStatus()==-2l){ 			// -2L表示      抓取失败
					vo.setSecondFail(entity.getFailNum());
				}else if(entity.getStatus()==-3l){ 	//-3表示    解析失败  
					vo.setThirdFail(entity.getFailNum());
				}else if(entity.getStatus()==-4l){		//-4表示     其他失败
					vo.setForthFail(entity.getFailNum());
				}else if(entity.getStatus()==-10l){	//-10表示  示定义失败
					vo.setOtherFail(entity.getFailNum());
				}else if(entity.getStatus()==0l){      //  0 表示 等待
					vo.setWaiting(entity.getFailNum());
				}else if(entity.getStatus()== 1l){      //　1　启动中
					vo.setStarting(entity.getFailNum());
				}else if(entity.getStatus()==2l){      // 2  执行中
					vo.setWorking(entity.getFailNum());
				}
				
				listVO.add(vo);
			}
			
//			return listVO;
			return pingjie2(listVO);
		}
		
		//pingjie2
		//pingjie
		private List pingjie2(List<StatisticsTotalDetailEntityVO> list){
			Map<String,StatisticsTotalDetailEntityVO> map=new LinkedHashMap<String,StatisticsTotalDetailEntityVO>();
			for (int i = 0; i < list.size(); i++) {
				StatisticsTotalDetailEntityVO old=list.get(i);
				String key=old.getGrabdate()+"--"+old.getFromCity()+"--"+old.getFlightDate()+"--"+old.getToCity();//拼装成一个key
				if(map.get( key )!=null){//判断已经存在同个日期，同个渠道，成功数相同，实际抓取数相同，同一出发城市，同一到到达城市 的数据
					int k=1;
					k++;
					StatisticsTotalDetailEntityVO newVo=map.get(key);//将存在的数据取出来
					  //累加
					  Long total=nullToLong(old.getTotal(),newVo.getTotal());
					  
					  Long failNum=nullToLong(old.getFailNum(),newVo.getFailNum());
					  Long secondFail=nullToLong(old.getSecondFail(),newVo.getSecondFail());
					  Long thirdFail=nullToLong(old.getThirdFail(),newVo.getThirdFail());
					  Long forthFail=nullToLong(old.getForthFail(),newVo.getForthFail());
					  Long otherFail=nullToLong(old.getOtherFail(),newVo.getOtherFail());
					  Long waiting=nullToLong(old.getWaiting(),newVo.getWaiting());
					  Long starting=nullToLong(old.getStarting(),newVo.getStarting());
					  Long working=nullToLong(old.getWorking(),newVo.getWorking());
					  Long actualNum=nullToLong(old.getActualNum(), newVo.getActualNum());
					  Long successNum=nullToLong(old.getSuccessNum(), newVo.getSuccessNum());
					  Long maxTime=nullToLong(old.getMaxTime(), newVo.getMaxTime());
					  Long minTime=nullToLong(old.getMinTime(), newVo.getMinTime());
					  newVo.setTotal(total);//total在数据库中多加了一遍成功数
					  newVo.setFailNum(failNum);
					  newVo.setSecondFail(secondFail); 
					  newVo.setThirdFail(thirdFail);
					  newVo.setForthFail(forthFail);
					  newVo.setOtherFail(otherFail);
					  newVo.setWaiting(waiting);
					  newVo.setWorking(working);
					  newVo.setStarting(starting);
					  newVo.setMaxTime(maxTime/k);
					  newVo.setMinTime(minTime/k);
					  newVo.setActualNum(actualNum/k);
					  newVo.setSuccessNum(successNum);
					  
					  
		              map.put(key,newVo);
		       }else{
		          map.put(key,old);
		       }
			}
			Object s[] = map.keySet().toArray();
			for(int i = 0; i < map.size(); i++) {
				StatisticsTotalDetailEntityVO stu=map.get(s[i]);
			}
			List voList = new ArrayList();
			Iterator iter = map.entrySet().iterator();  //获得map的Iterator
			while(iter.hasNext()) {
				Entry entry = (Entry)iter.next();
				voList.add(entry.getValue());
			}
			return voList;
		}
		
	//引擎统计 ，total失败详细信息
		//getTotalRecordByDateAndChannel
		public List<TaskRecordEntityVO> getTotalRecordByDateAndChannel(
				Date grabdate, String grabchannelID,String status,String fromCity,String toCity,int page ,int pagesize) {
			TaskQueueLogEntity tqe = new TaskQueueLogEntity();
			List<TaskRecordEntityVO> listVO=new ArrayList<TaskRecordEntityVO>();
			List<TaskQueueLogEntity> list=new ArrayList<TaskQueueLogEntity>();
			list=tqe.getTotalRecordByDateAndChannel(grabdate, grabchannelID,status,fromCity,toCity,page ,pagesize);
			for (TaskQueueLogEntity entity : list) {
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo.setByteLength(entity.getByteLength());
				vo.setCrawlCount(entity.getCrawlCount());
				
				vo.setEndTime(entity.getEndTime());
				vo.setFromCityOrg(entity.getFromCity());
				vo.setFromCityName(entity.getFromCityName());
				vo.setGrabChannel(entity.getGrabChannel());
				vo.setGrabDate(entity.getFlightDate());
				vo.setRecordName(entity.getRecordName());
				vo.setRemarks(entity.getRemarks());
				vo.setStartTime(entity.getStartTime());
				vo.setFlightDate(entity.getFlightDate());
				vo.setReturnGrabDate(entity.getReturnGrabDate());
				vo.setStatus(entity.getStatus()+"");
				vo.setTaskid(entity.getTaskId());
				vo.setUuid(entity.getId());
				vo.setToCityOrg(entity.getToCity());
				vo.setToCityName(entity.getToCityName());
				vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
						entity.getToCityName()+"("+entity.getToCity()+")");
				vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
				vo.setSourceRowkey(entity.getSourceRowkey());
				vo.setSourceDataSize(entity.getSourceDataSize());
				listVO.add(vo);
			}
			
			return listVO;
		}
		
		//tatol and fail   getTotalAndFailRecordByDateAndChannel
		//for WS
		/*@Override
		public List<TaskRecordEntityVO> getTotalAndFailRecordByByWS(TaskModel[] models) {
			int modelSizes=models.length;
			if(modelSizes>0){
				List<TaskRecordEntityVO> listVo =new ArrayList<TaskRecordEntityVO>();
				for(TaskModel entity:models){
					TaskRecordEntityVO vo=new TaskRecordEntityVO();
					if(entity.getStatus() !=3){
						vo.setFlightDate(entity.getFlightDate());
						vo.setFromCityName(entity.getFromCityName());
						vo.setByteLength(entity.getByteLength());
						vo.setCrawlCount(entity.getCrawlCount());
						vo.setEndTime(entity.getEndTime());
						vo.setFromCityOrg(entity.getFromCity());
						vo.setFromCityName(entity.getFromCityName());
						vo.setGrabChannel(entity.getGrabChannel());
						vo.setGrabDate(entity.getFlightDate());
						vo.setRecordName(entity.getRecordName());
						vo.setRemarks(entity.getRemarks());
						vo.setStartTime(entity.getStartTime());
						vo.setFlightDate(entity.getFlightDate());
						vo.setReturnGrabDate(entity.getReturnGrabDate());
						vo.setStatus(entity.getStatus()+"");
						vo.setUuid(entity.getId());
						vo.setToCityOrg(entity.getToCity());
						vo.setToCityName(entity.getToCityName());
						vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
								entity.getToCityName()+"("+entity.getToCity()+")");
						vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
						vo.setSourceRowkey(entity.getSourceRowkey());
						vo.setSourceDataSize(entity.getSourceDataSize().longValue());
						
						listVo.add(vo);
					}
				}
				return listVo;
			}
			return null;
		}*/
		//for DB
		@Override
		public List<TaskRecordEntityVO> getTotalAndFailRecordByDateAndChannel(
				Date grabdate, String grabchannelID,String fromCity,String toCity,int page ,int pagesize) {
			TaskQueueLogEntity tqe = new TaskQueueLogEntity();
			List<TaskRecordEntityVO> listVO=new ArrayList<TaskRecordEntityVO>();
			List<TaskQueueLogEntity> list=new ArrayList<TaskQueueLogEntity>();
			list=tqe.getTotalAndFailRecordByDateAndChannel(grabdate, grabchannelID,fromCity,toCity,page ,pagesize);
			for (TaskQueueLogEntity entity : list) {
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo.setByteLength(entity.getByteLength());
				vo.setCrawlCount(entity.getCrawlCount());
				vo.setEndTime(entity.getEndTime());
				vo.setFromCityOrg(entity.getFromCity());
				vo.setFromCityName(entity.getFromCityName());
				vo.setGrabChannel(entity.getGrabChannel());
				vo.setGrabDate(entity.getFlightDate());
				vo.setRecordName(entity.getRecordName());
				vo.setRemarks(entity.getRemarks());
				vo.setStartTime(entity.getStartTime());
				vo.setFlightDate(entity.getFlightDate());
				vo.setReturnGrabDate(entity.getReturnGrabDate());
				vo.setStatus(entity.getStatus()+"");
				vo.setTaskid(entity.getTaskId());
				vo.setUuid(entity.getId());
				vo.setToCityOrg(entity.getToCity());
				vo.setToCityName(entity.getToCityName());
				vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
						entity.getToCityName()+"("+entity.getToCity()+")");
				vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
				vo.setSourceRowkey(entity.getSourceRowkey());
				vo.setSourceDataSize(entity.getSourceDataSize());
				listVO.add(vo);
			}
			return listVO;
		}
		
	//引擎统计 ，失败详细信息
	@Override
	public List<TaskRecordEntityVO> getTaskRecordByDateAndChannel(
			Date grabdate, String grabchannelID,String status,int page ,int pagesize) {
		TaskQueueLogEntity tqe = new TaskQueueLogEntity();
		List<TaskRecordEntityVO> listVO=new ArrayList<TaskRecordEntityVO>();
		List<TaskQueueLogEntity> list=new ArrayList<TaskQueueLogEntity>();
		list=tqe.getTaskRecordByDateAndChannel(grabdate, grabchannelID,status,page ,pagesize);
		for (TaskQueueLogEntity entity : list) {
			TaskRecordEntityVO vo=new TaskRecordEntityVO();
			vo.setByteLength(entity.getByteLength());
			vo.setCrawlCount(entity.getCrawlCount());
			vo.setEndTime(entity.getEndTime());
			vo.setFromCityOrg(entity.getFromCity());
			vo.setFromCityName(entity.getFromCityName());
			vo.setGrabChannel(entity.getGrabChannel());
			vo.setGrabDate(entity.getFlightDate());
			vo.setRecordName(entity.getRecordName());
			vo.setRemarks(entity.getRemarks());
			vo.setStartTime(entity.getStartTime());
			vo.setStatus(entity.getStatus()+"");
			vo.setTaskid(entity.getTaskId());
			vo.setUuid(entity.getId());
			vo.setToCityOrg(entity.getToCity());
			vo.setToCityName(entity.getToCityName());
			vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
					entity.getToCityName()+"("+entity.getToCity()+")");
			vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
			vo.setSourceRowkey(entity.getSourceRowkey());
			vo.setSourceDataSize(entity.getSourceDataSize());
			listVO.add(vo);
		}
		
		return listVO;
	}

	@Override
	public int getCountByDateAndChannel(Date grabdate, String grabchannelID,String status) {
		TaskQueueLogEntity tqe = new TaskQueueLogEntity();
		return tqe.getCountByDateAndChannel(grabdate, grabchannelID,status);
	}
	//fail and total  
	public int getTotalAndFailCountByDateAndChannel(Date grabdate, String grabchannelID,String fromCity, String toCity) {
		TaskQueueLogEntity tqe = new TaskQueueLogEntity();
		return tqe.getTotalAndFailCountByDateAndChannel(grabdate, grabchannelID,fromCity, toCity);
	}
	
	
	//Total 总数失败中的详细信息 
	public int getTotalCountByDateAndChannel(Date grabdate, String grabchannelID,String status ,String fromCity, String toCity) {
		TaskQueueLogEntity tqe = new TaskQueueLogEntity();
		return tqe.getTotalCountByDateAndChannel(grabdate, grabchannelID,status,fromCity, toCity);
	}
	
	//引擎统计——总数
	public int getTotalCountByFromAndTo(Date grabdate, String grabchannelID,String fromCity, String toCity) {
		StatisticsTotalDetailEntity tqe = new StatisticsTotalDetailEntity();
		return tqe.getTotalCountByFromAndTo(grabdate, grabchannelID,fromCity, toCity);
	}
	//total
	public int getCountByDateAndChannel_2(String grabdate, String grabchannelID) {
		StatisticsTotalEntity tqe = new StatisticsTotalEntity();
		return tqe.getCountByDateAndChannel_2(grabdate, grabchannelID);
	}


	@Override
	public List<TaskRecordEntityVO> findTaskRecordByCondition(
			String fromCityOrg, String toCityOrg, Date startTime, Date endTime,
			String status,String grabchannel) {
		return null;
	}

	@Override
	public List<TaskQueueLogEntity> getTaskRecordByNowDate(Date nowDate) {
		return new TaskQueueLogEntity().getTaskRecordByNowDate(nowDate);
	}




	/**
	 * 持久化数据查询
	 * @return
	*/
	@Override
	public List<TaskRecordEntityVO> getAllDatasByGrabDate(String grabDateStr,String endGrabDateStr) {
		TaskQueueLogEntity tp=new TaskQueueLogEntity();
		Date grabDates=DateUtils.strToDate(grabDateStr, "yyyy-MM-dd HH:mm:ss");
		Date endGrabDates=DateUtils.strToDate(endGrabDateStr, "yyyy-MM-dd HH:mm:ss");
		
		List<TaskQueueLogEntity> listEnity =tp.getAllDatasByGrabDate(grabDates,endGrabDates);
		List<TaskRecordEntityVO> listVo=new ArrayList<TaskRecordEntityVO>();
		if(listEnity.size()>0){
			for(TaskQueueLogEntity entity:listEnity){
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo.setGrabDate(DateUtils.dateToStr(entity.getCreateTime(), "yyyy-MM-dd HH"));
				vo.setCreateTime(new Date());
				vo.setFromCityName(entity.getFromCityName());
				vo.setFromCityOrg(entity.getFromCity());
				vo.setToCityName(entity.getToCityName());
				vo.setToCityOrg(entity.getToCity());
				vo.setSourceDataSize(entity.getSourceDataSize());
				vo.setGrabChannel(entity.getGrabChannel());
				vo.setGrabchannelID(entity.getGrabchannelID());
				vo.setSourceRowkey(entity.getHbaseKey());
				vo.setStatus(entity.getStatus()+"");
				vo.setFlightDate(entity.getFlightDate());
				
				listVo.add(vo);
			}
		}
		
		
		
		
		return listVo;
	}


}
